<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@page import="weaver.general.BaseBean" %>
<%@page import="weaver.conn.RecordSet" %>
<%@page import="weaver.general.Util" %>
<%@page import="weaver.conn.RecordSetDataSource" %>
<%@page import="weaver.jobs.common.DocUtil" %>
<%@page import="weaver.hrm.User" %>
<%@page import="weaver.hrm.HrmUserVarify" %>
<%@page import="weaver.jobs.common.ModeUtil" %>
<%@page import="net.sf.json.JSONArray" %>
<%@page import="net.sf.json.JSONObject" %>
<%
    BaseBean bb = new BaseBean();
    RecordSet rs = new RecordSet();
    User user = HrmUserVarify.getUser( request, response );
    ModeUtil modeUtil = new ModeUtil();

    int formmodeid = 2;
    String tablename = "";
    String modedatacreatertype = "1";
    String seccategory = "1508";
    String source = "oldoa";
    String subcompanyid1 = "1";
    String downloadpath = "http://192.168.15.80/netoffice//Pages/AttachStorageInfo/ReadAttachFile.aspx?AttachGuid=";

    String modesql = "select a.*,b.tablename from modeinfo a,workflow_bill b where a.formid=b.id and a.id=" + formmodeid;
    rs.executeSql( modesql );
    while (rs.next()) {
        String id = Util.null2String( rs.getString( "seccategory" ) );
        tablename = Util.null2String( rs.getString( "tablename" ) );
        if (!id.equals( "" ) && !id.equals( "0" )) {
            seccategory = id;
        }
    }

    Map<String, String> userMap = new HashMap();
    Map<String, String> deptMap = new HashMap();
    Map<String, String> compMap = new HashMap();

    String sql = "select * from hrmresource where subcompanyid1=" + subcompanyid1;
    rs.executeSql( sql );
    while (rs.next()) {
        String id = Util.null2String( rs.getString( "id" ) );
        String lastname = Util.null2String( rs.getString( "lastname" ) );
        userMap.put( lastname, id );
    }

    sql = "select * from hrmdepartment where subcompanyid1=" + subcompanyid1;
    rs.executeSql( sql );
    while (rs.next()) {
        String id = Util.null2String( rs.getString( "id" ) );
        String departmentname = Util.null2String( rs.getString( "departmentname" ) );
        deptMap.put( departmentname, id );
    }

    sql = "select * from hrmsubcompany";
    rs.executeSql( sql );
    while (rs.next()) {
        String id = Util.null2String( rs.getString( "id" ) );
        String subcompanyname = Util.null2String( rs.getString( "subcompanyname" ) );
        compMap.put( subcompanyname, id );
    }

    List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
    Map<String, Object> dataMap = null;
    Map<String, String> fileMap = new HashMap<String, String>();
    Map<String, String> mainidMap = new HashMap<String, String>();

    RecordSetDataSource rsd = new RecordSetDataSource( source );

//String wheresql = " and OperateDate<='2019-06-31' ";
//String wheresql = " and rowguid='2076628e-4bb0-430f-a237-517ab13f304a' ";
    String wheresql = "";
    sql = "select t1.*,t2.ouname from wd_24 t1,frame_ou t2 " +
            "where t1.SubWebFlowOuGuid = t2.ouguid " +
            wheresql +
            //"and rowguid='d523a510-43b5-4e05-aad5-4e7ca8406b18' " +
            "order by OperateDate desc";
   // out.print( "获取发文数据sql：" + sql );
    rsd.execute( sql );
    while (rsd.next()) {
        String JpdFawendz = Util.null2String( rsd.getString( "JpdFawendz" ) );//代字 锡交办发
        String JpdFawenYear = Util.null2String( rsd.getString( "JpdFawenYear" ) );//年号 2021
        String FaWenFileNumber = Util.null2String( rsd.getString( "FaWenFileNumber" ) );//年号 〔2021〕号
        String TxtFileNumber = Util.null2String( rsd.getString( "TxtFileNumber" ) );//字号 12
        String TxtTitle = Util.null2String( rsd.getString( "TxtTitle" ) );//标题
        String JpdMiJi = Util.null2String( rsd.getString( "JpdMiJi" ) );//密级
        String JpdHuanJi = Util.null2String( rsd.getString( "JpdHuanJi" ) );//紧急程度
        String JpdBaoCunQX = Util.null2String( rsd.getString( "JpdBaoCunQX" ) );//保管期限
        String JpdGuiDangLeiMu = Util.null2String( rsd.getString( "JpdGuiDangLeiMu" ) );//归档类目
        String OperateDate = Util.null2String( rsd.getString( "OperateDate" ) );//操作时间
        String RowGuid = Util.null2String( rsd.getString( "RowGuid" ) );//
        String InfoOpen = Util.null2String( rsd.getString( "InfoOpen" ) );//信息公开
        String ProcessVersionInstanceGuid = Util.null2String( rsd.getString( "ProcessVersionInstanceGuid" ) );//
        String InitUserDisplayName = Util.null2String( rsd.getString( "InitUserDisplayName" ) );//拟稿人
        String inducestate = Util.null2String( rsd.getString( "inducestate" ) );//
        String zhusong = Util.null2String( rsd.getString( "zhusongguid" ) );//主送
        String chaosong = Util.null2String( rsd.getString( "chaosongguid" ) );//抄送
        String zhutici = Util.null2String( rsd.getString( "zhutici" ) );//主题词
        String hostouname = Util.null2String( rsd.getString( "hostouname" ) );//
        String ouname = Util.null2String( rsd.getString( "ouname" ) );//
        String czrq = "";
        String czsj = "";
        if (!"".equals( OperateDate )) {
            czrq = OperateDate.substring( 0, 10 );
            czsj = OperateDate.substring( 11, 16 );
        }

        dataMap = new HashMap<String, Object>();
        dataMap.put( "dz", JpdFawendz );
        dataMap.put( "nh", JpdFawenYear );
        dataMap.put( "nxh", FaWenFileNumber );
        dataMap.put( "zh", TxtFileNumber );
        dataMap.put( "bt", TxtTitle );
        dataMap.put( "mj", JpdMiJi );
        dataMap.put( "jjcd", JpdHuanJi );
        dataMap.put( "bgqx", JpdBaoCunQX );
        dataMap.put( "gdlm", JpdGuiDangLeiMu );
        dataMap.put( "czrq", czrq );
        dataMap.put( "czsj", czsj );
        dataMap.put( "rowguid", RowGuid );
        dataMap.put( "xxgk", InfoOpen );
        dataMap.put( "ngr", InitUserDisplayName );
        dataMap.put( "zt", inducestate );
        dataMap.put( "zs", zhusong );
        dataMap.put( "cs", chaosong );
        dataMap.put( "ztc", zhutici );
        dataMap.put( "dwmc", hostouname );
        dataMap.put( "dw", ouname );
       // out.print( "dataMap=" + dataMap.toString() + "<br/>" );
        dataList.add( dataMap );
        fileMap.put( RowGuid, ProcessVersionInstanceGuid );
    }

    Map<String, List<Map>> zws = new HashMap();
    Map<String, List<Map>> fjs = new HashMap();

    sql = "select attachguid,ATTACHFILENAME,FILEPATH,CLIENGTAG,CLIENGGUID from frame_attachinfo " +
            "where CLIENGGUID in (select ProcessVersionInstanceGuid from wd_24 where 1=1 " + wheresql + ") and 1=1 order by uploaddatetime";
    rsd.execute( sql );
    while (rsd.next()) {
        String attachguid = Util.null2String( rsd.getString( "attachguid" ) );
        String attachfilename = Util.null2String( rsd.getString( "attachfilename" ) );
        String cliengtag = Util.null2String( rsd.getString( "cliengtag" ) );
        String cliengguid = Util.null2String( rsd.getString( "cliengguid" ) );

        Map temp = new HashMap();
        temp.put( "attachguid", attachguid );
        temp.put( "attachfilename", attachfilename );
        temp.put( "cliengtag", cliengtag );

        if (cliengtag.equals( "zhangwai" )) {
            continue;
        } else if (cliengtag.equals( "formal" ) || cliengtag.equals( "origin" ) || cliengtag.equals( "revision" )) {
            List list = new ArrayList();
            //if(zws.containsKey(cliengguid)){
            //	list = zws.get(cliengguid);
            //}
            list.add( temp );
            zws.put( cliengguid, list );
        } else {
            List list = new ArrayList();
            if (fjs.containsKey( cliengguid )) {
                list = fjs.get( cliengguid );
            }
            list.add( temp );
            fjs.put( cliengguid, list );
        }
    }

    for (int i = 0; i < dataList.size(); i++) {
        Map<String, Object> dmap = dataList.get( i );
        String rowguid = Util.null2String( dmap.get( "rowguid" ) );
        String bt = Util.null2String( dmap.get( "bt" ) );
        String ProcessVersionInstanceGuid = Util.null2String( fileMap.get( rowguid ) );

        String zw = "";
       // out.print( "ProcessVersionInstanceGuid=" + ProcessVersionInstanceGuid + "<br/>" );
        if (zws.containsKey( ProcessVersionInstanceGuid )) {
            List<Map> zwlist = zws.get( ProcessVersionInstanceGuid );
            for (Map temp : zwlist) {
                String attachguid = Util.null2String( temp.get( "attachguid" ) );
                String attachfilename = Util.null2String( temp.get( "attachfilename" ) );
                String cliengtag = Util.null2String( temp.get( "cliengtag" ) );

                String downloadUrl = downloadpath + attachguid;
               // out.print( "<br>下载地址----->" + downloadUrl + "<br/>" );

                Map<String, Object> retMap = DocUtil.downloadByUrl( downloadUrl );
                if (!Util.null2String( retMap.get( "code" ) ).equals( "0" )) {
                   // out.print( "<br>" + attachfilename + "----->" + retMap.get( "msg" ) + "<br/>" );
                } else {
                    String filepath = Util.null2String( retMap.get( "data" ) );
                    int imagefileid = DocUtil.saveImageFile( attachfilename, filepath );

                    int docid = DocUtil.createDocByImagefileId( imagefileid, user, "1", "0", attachfilename, "", Util.getIntValue( seccategory, -1 ) );
                    zw += (zw.equals( "" ) ? docid : ("," + docid));
                }
            }

        }
        String fj = "";
        if (fjs.containsKey( ProcessVersionInstanceGuid )) {
            List<Map> fjlist = fjs.get( ProcessVersionInstanceGuid );
            for (Map temp : fjlist) {
                String attachguid = Util.null2String( temp.get( "attachguid" ) );
                String attachfilename = Util.null2String( temp.get( "attachfilename" ) );
                String cliengtag = Util.null2String( temp.get( "cliengtag" ) );

                String downloadUrl = downloadpath + attachguid;
               // out.print( "<br>下载地址----->" + downloadUrl + "<br/>" );

                Map<String, Object> retMap = DocUtil.downloadByUrl( downloadUrl );
                if (!Util.null2String( retMap.get( "code" ) ).equals( "0" )) {
                   // out.print( "<br>" + attachfilename + "----->" + retMap.get( "msg" ) + "<br/>" );
                } else {
                    String filepath = Util.null2String( retMap.get( "data" ) );
                    int imagefileid = DocUtil.saveImageFile( attachfilename, filepath );

                    int docid = DocUtil.createDocByImagefileId( imagefileid, user, "1", "0", attachfilename, "", Util.getIntValue( seccategory, -1 ) );
                    fj += (fj.equals( "" ) ? docid : ("," + docid));
                }
            }
        }

        if (!zw.equals( "" )) {
            dmap.put( "zw", zw );
        }
        if (!fj.equals( "" )) {
            dmap.put( "fj", fj );
        }

        Map<String, Object> retMap = modeUtil.addModeInfo( dmap, tablename, formmodeid, user.getUID(), false, true, "rowguid" );
        if (!Util.null2String( retMap.get( "code" ) ).equals( "0" )) {
            //out.print( i + "--->【" + bt + "】:失败，失败原因：" + Util.null2String( retMap.get( "msg" ) ) );
        } else {
            //out.print( i + "--->【" + bt + "】:成功，id：" + Util.null2String( retMap.get( "data" ) ) );

            mainidMap.put( rowguid, Util.null2String( retMap.get( "data" ) ) );
        }
    }

    Map<String, List<Map>> logs = new HashMap();
    sql = " select  workitemguid,activityname,sendername,transactorname,operationtype," +
            "     createdate,enddate,opinion,workitemtype,processVersionInstanceGuid " +
            "  from workflow_workitem_history " +
            " where processVersionInstanceGuid in (select processVersionInstanceGuid from wd_24 where 1=1 " + wheresql + ") ";
    rsd.execute( sql );
    while (rsd.next()) {
        String processVersionInstanceGuid = Util.null2String( rsd.getString( "processVersionInstanceGuid" ) );
        String uuid = Util.null2String( rsd.getString( "workitemguid" ) );
        String bz = Util.null2String( rsd.getString( "activityname" ) );
        String sybtjr = Util.null2String( rsd.getString( "sendername" ) );
        String clry = Util.null2String( rsd.getString( "transactorname" ) );
        String czlx = Util.null2String( rsd.getString( "operationtype" ) );
        String createdate = Util.null2String( rsd.getString( "createdate" ) );
        String enddate = Util.null2String( rsd.getString( "enddate" ) );
        String clyj = Util.null2String( rsd.getString( "opinion" ) );

        Map temp = new HashMap();
        temp.put( "workitemguid", uuid );
        temp.put( "activityname", bz );
        temp.put( "sendername", sybtjr );
        temp.put( "transactorname", clry );
        temp.put( "operationtype", czlx );
        temp.put( "createdate", createdate );
        temp.put( "enddate", enddate );
        temp.put( "opinion", clyj );

        List<Map> list = new ArrayList();
        if (logs.containsKey( processVersionInstanceGuid )) {
            list = logs.get( processVersionInstanceGuid );
        }
        list.add( temp );
        logs.put( processVersionInstanceGuid, list );
    }

    sql = " select  workitemguid,activityname,sendername,transactorname,operationtype," +
            "     createdate,enddate,opinion,workitemtype,processVersionInstanceGuid " +
            "  from workflow_workitem " +
            " where processVersionInstanceGuid in (select processVersionInstanceGuid from wd_24 where 1=1 " + wheresql + ") ";
    rsd.execute( sql );
    while (rsd.next()) {
        String processVersionInstanceGuid = Util.null2String( rsd.getString( "processVersionInstanceGuid" ) );
        String uuid = Util.null2String( rsd.getString( "workitemguid" ) );
        String bz = Util.null2String( rsd.getString( "activityname" ) );
        String sybtjr = Util.null2String( rsd.getString( "sendername" ) );
        String clry = Util.null2String( rsd.getString( "transactorname" ) );
        String czlx = Util.null2String( rsd.getString( "operationtype" ) );
        String createdate = Util.null2String( rsd.getString( "createdate" ) );
        String enddate = Util.null2String( rsd.getString( "enddate" ) );
        String clyj = Util.null2String( rsd.getString( "opinion" ) );

        Map temp = new HashMap();
        temp.put( "workitemguid", uuid );
        temp.put( "activityname", bz );
        temp.put( "sendername", sybtjr );
        temp.put( "transactorname", clry );
        temp.put( "operationtype", czlx );
        temp.put( "createdate", createdate );
        temp.put( "enddate", enddate );
        temp.put( "opinion", clyj );

        List<Map> list = new ArrayList();
        if (logs.containsKey( processVersionInstanceGuid )) {
            list = logs.get( processVersionInstanceGuid );
        }
        list.add( temp );
        logs.put( processVersionInstanceGuid, list );
    }

    for (int i = 0; i < dataList.size(); i++) {
        Map<String, Object> dmap = dataList.get( i );
        String rowguid = Util.null2String( dmap.get( "rowguid" ) );
        String mainid = Util.null2String( mainidMap.get( rowguid ) );
        String processVersionInstanceGuid = Util.null2String( fileMap.get(rowguid) );
        sql = "delete from " + tablename + "_dt1 where mainid='" + mainid + "'";
        rs.executeSql( sql );
        if (!logs.containsKey( processVersionInstanceGuid )) {
            continue;
        }
        List<Map> list = logs.get( processVersionInstanceGuid );

        for (Map temp : list) {
            String uuid = Util.null2String( temp.get( "workitemguid" ) );
            String bz = Util.null2String( temp.get( "activityname" ) );
            String sybtjr = Util.null2String( temp.get( "sendername" ) );
            String clry = Util.null2String( temp.get( "transactorname" ) );
            String sybtjryid = "";
            String clryid = "";

            if (!"".equals( sybtjr )) {
                sybtjryid = Util.null2String( userMap.get( sybtjr ) );
            }
            if (!"".equals( clry )) {
                clryid = Util.null2String( userMap.get( clry ) );
            }

            String czlx = Util.null2String( temp.get( "operationtype" ) );
            String createdate = Util.null2String( temp.get( "createdate" ) );

            String sdsj = "";
            String sdrq = "";
            if (!"".equals( createdate )) {
                sdrq = createdate.substring( 0, 10 );
                sdsj = createdate.substring( 11, 16 );
            }
            String clsj = "";
            String clrq = "";
            String enddate = Util.null2String( temp.get( "enddate" ) );
            if (!"".equals( enddate )) {
                clrq = enddate.substring( 0, 10 );
                clsj = enddate.substring( 11, 16 );
            }

            String clyj = Util.null2String( temp.get( "opinion" ) );
            Map<String, Object> detailMap = new HashMap<String, Object>();
            detailMap.put( "uuid", uuid );
            detailMap.put( "mainid", mainid );
            detailMap.put( "bz", bz );
            detailMap.put( "sybtjry", sybtjr );
            detailMap.put( "clry", clry );

            detailMap.put( "sybtjryid", sybtjryid );
            detailMap.put( "clryid", clryid );
            detailMap.put( "czlx", czlx );
            detailMap.put( "sdsj", sdsj );
            detailMap.put( "sdrq", sdrq );
            detailMap.put( "clsj", clsj );
            detailMap.put( "clrq", clrq );
            detailMap.put( "clyj", clyj );

            modeUtil.insertsql( detailMap, tablename + "_dt1" );
        }
    }
%>
<%!
    public void writeLog(Object obj) {
        if (true) {
            new BaseBean().writeLog( this.getClass().getName(), obj );
        }
    }
%>